package com.googlecode.jmicroerp.wicket.administrare;

import com.googlecode.jmicroerp.model.Magazin;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author alex
 */
public class EditareMagazinePage extends DefaultEditPage {
    private Magazin magazin;
    @SpringBean(name = "genericService")
    private GenericService<Magazin> genericService;
    
    public EditareMagazinePage() {
        magazin = new Magazin();
        init();
    }
    
    public EditareMagazinePage(Long id) {
        magazin = genericService.getById(Magazin.class, id);
        init();
    }
    
    private void init() {
        Form<Magazin> form = new Form("form");
        form.setModel(new CompoundPropertyModel<Magazin>(magazin));
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        TextField<String> tfAdresa = new TextField<String>("adresa");
        form.add(tfAdresa);
        
        form.add(new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (magazin.getId() == null) {
                    genericService.insert(magazin);
                } else {
                    genericService.update(magazin);
                }
                setResponsePage(VizualizareMagazinePage.class);
            }
            
        });
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(VizualizareMagazinePage.class);
            }
            
        });
        
        add(form);
    }
}
